﻿using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Linq;

namespace Delsjö
{
    public partial class MainPage : PhoneApplicationPage
    {
        private ShellTileData CreateIconicTileData()
        {
            IconicTileData iconicTileData = new IconicTileData();
            iconicTileData.Count = 11;
            iconicTileData.IconImage = new Uri("/Assets/IconicTileMediumLarge.png", UriKind.Relative);
            iconicTileData.SmallIconImage = new Uri("/Assets/IconicTileSmall.png", UriKind.Relative);
            iconicTileData.WideContent1 = "Till Delsjömotet";
            iconicTileData.WideContent2 = "18:30->18:54";
            iconicTileData.WideContent3 = "RÖD Express";
            return iconicTileData;
        }

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;

            //ShellTile tile = ShellTile.ActiveTiles.First();

            /*
            IconicTileData iconicTileData = new IconicTileData()
            {
               Title = "Delsjömotet",
               Count = 12,
               WideContent1 = "sdfg2345234",
               WideContent2 = "abrakadabsdf",
               WideContent3 = "123123123123",
               SmallIconImage = ,
               IconImage = [medium/wide Tile size URI],
               BackgroundColor = [.NET color type of Tile]
            };
             */
            //ShellTileData tileData = CreateIconicTileData();
            //ShellTileSchedule tileSchedule = new ShellTileSchedule(tile, tileData);
            //tileSchedule.Interval = UpdateInterval.EveryHour;
            //tileSchedule.Recurrence = UpdateRecurrence.Interval;
            //tile.Update();
            

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
/*
                if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
                {
                    // User has opted in or out of Location
                    return;
                }
                else
                {
                    MessageBoxResult result =
                        MessageBox.Show("Kan jag få använda din platsinformation?",
                        "Plats",
                        MessageBoxButton.OKCancel);

                    if (result == MessageBoxResult.OK)
                    {
                        IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
                    }
                    else
                    {
                        IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
                    }

                    IsolatedStorageSettings.ApplicationSettings.Save();
                }
*/

                if (!App.ViewModel.IsDataLoaded)
                {
                    App.ViewModel.GetTrips();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (List.SelectedItem == null)
                return;

            // TODO::: Do something here?

            List.SelectedItem = null;

        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}